package com.dg11185.nearshop.view.adapter;

import java.util.List;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.nearshop.R;
import com.dg11185.libs.imageloader.core.DisplayImageOptions;
import com.dg11185.libs.imageloader.core.ImageLoader;
import com.dg11185.libs.imageloader.core.display.FadeInBitmapDisplayer;
import com.dg11185.nearshop.app.ConvertUnit;
import com.dg11185.nearshop.net.entity.Comment;

public class CommmentsAdapter extends BaseAdapter {

    private LayoutInflater inflater = null;

    private List<Comment> comments = null;

    private DisplayImageOptions options = null;

    /**
     * 构造函数
     * 
     * @param context
     * @param shops
     *            需要保证数据被初始化，如果使用set函数初始化BaseAdapter可能会报空指针；
     */
    public CommmentsAdapter(Context context, List<Comment> comments) {
        inflater = LayoutInflater.from(context);
        this.comments = comments;
        options = new DisplayImageOptions.Builder().cacheInMemory().showImageForEmptyUri(R.drawable.icon_user_icon_bg)
                .resetViewBeforeLoading().displayer(new FadeInBitmapDisplayer(500)).build();
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    @Override
    public int getCount() {
        return comments.size();
    }

    @Override
    public Object getItem(int position) {
        return comments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        Comment unit = comments.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.shop_detail_comment_item, parent, false);
            holder.headPhoto = (ImageView) convertView.findViewById(R.id.comment_photo);
            holder.time = (TextView) convertView.findViewById(R.id.comment_time1);
            holder.content = (TextView) convertView.findViewById(R.id.comment_content);
            holder.moreReply = (TextView) convertView.findViewById(R.id.comment_more_reply);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        String content = "<font color=#EC0C65> "+unit.userName +" ：    "+ "</font><font color=#000000>" +unit.title +"  "+unit.content+" </font>";
        String comment = "<font color=#999999>评论(</font><font color=#EC0C65>" +unit.totalComments +"</font><font color=#999999>)</font>";
        ImageLoader.getInstance().displayImage(unit.headPhoto, holder.headPhoto, options);
        holder.time.setText(ConvertUnit.dateConvert(unit.createTime));
        holder.content.setText(Html.fromHtml(content));
        holder.moreReply.setText(Html.fromHtml(comment));
        
        return convertView;
    }

    private class ViewHolder {

        private ImageView headPhoto;

        private TextView content;

        private TextView time;

        private TextView moreReply;
    }
}
